<template>
	<view class="user">
		<view class="head" :style="{height: pHeight + tHeight + 'px'}">
			<view class="xztitle" :style="[{height: pHeight + 'px', top: tHeight + 'px',lineHeight: pHeight + 'px'}]">
				资金管理
				<view class="iconfont icon-dayuhao1" @click="fanhui"></view>
			</view>
		</view>
		<view class="headtop" :style="{marginTop: pHeight + tHeight + 'px'}"></view>
		<view class="beijing">
			<image :src="baseUrl + '/wximage/fundbj.png'"></image>
			
			<view class="topcontent">
				<view class="top clearfix">
					<view class="title fl">账户余额</view>
					<view class="tixian fl" @click="topage('/subpackageA/user/fund/cash')">提现账户</view>
					<view class="jilu fl" @click="topage('/subpackageA/user/fund/cashrecord')">提现记录</view>
				</view>
				<view class="money">¥{{money}}</view>
				<view class="bottom clearfix">
					<view class="kuai fl">
						<view class="shuzi">{{shouyi.djsmoney}}</view>
						<view class="desc">待结算(元)</view>
					</view>
					<view class="kuai fl" @click="topage('/subpackageA/user/fund/cash')">
						<view class="shuzi">{{shouyi.ktxmoney}}</view>
						<view class="desc">待提现(元)</view>
					</view>
				</view>
			</view>
		</view>
		<view class="mendian" v-if="list.length != 0">
			<view class="heads clearfix">
				<view class="left fl">
					<image :src="baseUrl + '/wximage/hktubiao.png'"></image>
					<view class="name">总收入：{{shouyi.zmoney}}元</view>
				</view>
				<view class="right fr" @click="sxstatus = true">
					<image :src="baseUrl + '/wximage/shaixuan.png'"></image>
					<view class="name">{{sxlabel}}</view>
				</view>
				<u-select v-model="sxstatus" :list="sxlist" @confirm="confirmsx"></u-select>
			</view>
			<view class="date" @click="show = true">{{sdate}}至{{edate}}</view>
			<u-calendar v-model="show" :mode="mode" toolTip="时间选择" @change="change"></u-calendar>
			<view class="shuju" v-if="list.length != 0">
				<view class="li clearfix" v-for="(item,index) in list" :key="index" @click="topage('/subpackageA/user/fund/detail?id=' + item.id)">
					<view class="cen fl">
						<text class="title" v-if="item.pingtai == 0">微信-订单收入</text>
						<text class="title" v-if="item.pingtai == 1">支付宝-订单收入</text>
						<text class="time">{{item.create_time}}</text>
					</view>
					<view class="name fl" v-if="role_id == 1">+{{item.platform_money}}</view>
					<view class="name fl" v-if="role_id == 11">+{{item.agent_money}}</view>
					<view class="name fl" v-if="role_id == 12">+{{item.bussiness_money}}</view>
					<view class="name fl" v-if="role_id == 13">+{{item.yuangong_money}}</view>
					<image :src="baseUrl + '/wximage/right.png'" class="right"></image>
				</view>
			</view>
			<view class="empty" v-else>
				<image :src="baseUrl + '/wximage/empty.png'"></image>
				<text>暂无数据</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl: getApp().globalData.baseUrl,
				headerTop: 0,
				pHeight: 0,
				tHeight: 0,
				show: false,
				mode: 'range',
				sdate: '',
				edate: '',
				sxstatus: false,
				sxlist: [{
					value: 0,
					label: '全部',
				}, {
					value: 1,
					label: '微信',
				}, {
					value: 2,
					label: '支付宝',
				}],
				sxvalue: 0,
				sxlabel: '筛选',
				agent_id: '',
				role_id: '',
				money: '0.00',
				page: 1,
				pageSize: 10,
				total: 0,
				list: [],
				shouyi: {
					djsmoney: '0.00',
					ktxmoney: '0.00',
				},
			}
		},
		onLoad() {
			// 头部距离
			let res = uni.getSystemInfoSync() ///微信api方法
			let titleH;
			if (res && res['system']) {
				// 判断是否是安卓操作系统 （标题栏苹果为44px,安卓为48px）
				if (res['system'].indexOf('Android') > 0) {
					titleH = 48
				} else {
					titleH = 46
				}
				this.headerTop = titleH + res['statusBarHeight']
				this.pHeight = titleH
				this.tHeight = res['statusBarHeight']
			}
			
			// 用户信息
			this.agent_id = uni.getStorageSync('agent_id');
			this.role_id = uni.getStorageSync('role_id');
			
			this.request({
				url: '/agent/users',
				data: {
					id: this.agent_id,
				},
				method: "POST"
			}).then(res => {
				if (res.data.code == 200) {
					this.money = res.data.data.money;
				} else {
					uni.showToast({
						title: '请求失败，请稍后再试',
						icon: 'none'
					})
				}
			})
			
			this.request({
				url: '/xcx/usersfund',
				data: {
					agent_id: this.agent_id,
					role_id: uni.getStorageSync('role_id'),
				},
				method: "POST"
			}).then(res => {
				if (res.data.code == 200) {
					this.shouyi = res.data.data
				} else {
					uni.showToast({
						title: '请求失败，请稍后再试',
						icon: 'none'
					})
				}
			})
			
			//初始化时间,展示近一周数据
			let times = this.getDate('lastOneWeek');
			this.sdate = times.startTime;
			this.edate = times.endTime;
			
		},
		onShow() {
			// 用户信息
			this.agent_id = uni.getStorageSync('agent_id');
			this.role_id = uni.getStorageSync('role_id');
			
			this.request({
				url: '/agent/users',
				data: {
					id: this.agent_id,
				},
				method: "POST"
			}).then(res => {
				if (res.data.code == 200) {
					this.money = res.data.data.money;
				} else {
					uni.showToast({
						title: '请求失败，请稍后再试',
						icon: 'none'
					})
				}
			})
			
			this.request({
				url: '/xcx/usersfund',
				data: {
					agent_id: this.agent_id,
					role_id: uni.getStorageSync('role_id'),
				},
				method: "POST"
			}).then(res => {
				if (res.data.code == 200) {
					this.shouyi = res.data.data
				} else {
					uni.showToast({
						title: '请求失败，请稍后再试',
						icon: 'none'
					})
				}
			})
		},
		onReachBottom() {
			if(this.total != this.list.length){
				this.page = this.page + 1;
				
				this.getList( this.sdate, this.edate, this.page, this.pageSize,this.sxvalue)
			}
		},
		methods: {
			getList(sdate,edate,page,pageSize,sxvalue) {
				this.request({
					url: '/orderx/orderliushui',
					data: {
						agent_id: uni.getStorageSync('agent_id'),
						sdate: sdate,
						edate: edate,
						page: page,
						pageSize: pageSize,
						role_id: uni.getStorageSync('role_id'),
						pingtai: sxvalue,
					},
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						this.total = res.data.data.total;
						this.list = this.list.concat(res.data.data.data);
					} else {
						uni.showToast({
							title: '请求失败，请稍后再试',
							icon: 'none'
						})
					}
				})
			},
			getShouyi(sdate,edate){
				this.request({
					url: '/orderx/liushuimoney',
					data: {
						agent_id: uni.getStorageSync('agent_id'),
						sdate: sdate,
						edate: edate,
						role_id: uni.getStorageSync('role_id'),
						attr: 1,
					},
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						this.shouyi = res.data.data;
					} else {
						uni.showToast({
							title: '请求失败，请稍后再试',
							icon: 'none'
						})
					}
				})
			},
			change(e) {
				this.sdate = e.startDate;
				this.edate = e.endDate;
				
				this.page = 1;
				this.pageSize = 10;
				this.list = [];
				
				this.getList( this.sdate, this.edate, this.page, this.pageSize,this.sxvalue)
				this.getShouyi( this.sdate, this.edate);
			},
			confirmsx(e) {
				this.sxvalue = e[0].value;
				this.sxlabel = e[0].label;
				
				//初始化时间,展示近一周数据
				let times = this.getDate('lastOneWeek');
				this.sdate = times.startTime;
				this.edate = times.endTime;
				this.page = 1;
				this.pageSize = 10;
				this.list = [];
				this.getList( this.sdate, this.edate, this.page, this.pageSize,this.sxvalue)
				this.getShouyi( this.sdate, this.edate);
			},
			//格式化时间
			formatTime(param) {
				let y = param.getFullYear();
				let m = param.getMonth() + 1;
				let d = param.getDate();
				m = m < 10 ? ("0" + m) : m;
				d = d < 10 ? ("0" + d) : d;
				return y + "-" + m + "-" + d;
			},
			getDate(code) {
				const date = new Date();
				let endTime = this.formatTime(date);
				let date1 = Date.parse(date);
				let start = '';
				let end = '';
				let oneDay = 1000 * 3600 * 24;
			
				switch (code) {
					//今天
					case 'today':
						start = new Date();
						break;
						//最近2天
					case 'lastTwoDay':
						start = date1 - oneDay * 2;
						break;
						//最近1周
					case 'lastOneWeek':
						start = date1 - oneDay * 7;
						break;
						//最近2周
					case 'lastTwoWeek':
						start = date1 - oneDay * 14;
						break;
						//最近1月
					case 'lastOneMonth':
						start = new Date();
						start.setMonth(start.getMonth() - 1)
						break;
						//最近2月
					case 'lastTwoMonth':
						start = new Date();
						start.setMonth(start.getMonth() - 2)
						break;
						//最近3月
					case 'lastThreeMonth':
						start = new Date();
						start.setMonth(start.getMonth() - 3)
						break;
						//最近半年
					case 'lastHalfYear':
						start = date1 - oneDay * 183;
						break;
						//最近一年
					case 'lastOneYear':
						start = new Date();
						start.setYear(start.getFullYear() - 1)
						break;
						//本周
					case 'thisWeek':
						let a = 6 - date.getDay();
						start = new Date(date1 - oneDay * a).setHours(0, 0, 0, 0);
						end = new Date(date1 + oneDay * (1 + date.getDay())).setHours(24, 0, 0, 0)
						break;
						//本月
					case 'thisMonth':
						start = new Date();
						start.setHours(0, 0, 0, 0)
						start.setMonth(start.getMonth(), 1)
						end = new Date(start)
						end.setHours(0, 0, 0, 0)
						end.setMonth(start.getMonth() + 1, 1)
						break;
						//本年
					case 'thisYear':
						start = new Date();
						start.setHours(0, 0, 0, 0)
						start.setMonth(0, 1)
						start.setYear(start.getFullYear())
						end = new Date(start)
						end.setHours(0, 0, 0, 0)
						end.setMonth(start.getMonth(), 1)
						end.setYear(start.getFullYear() + 1)
						break;
				}
			
				return {
					startTime: this.formatTime(new Date(start)),
					endTime: end ? this.formatTime(new Date(end)) : endTime,
				}
			},
			topage(url){
				uni.navigateTo({
					url
				})
			},
			fanhui() {
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: #f9f9ff;
	}
	
	.user {
		width: 100%;
		margin: 0 auto;

		.head {
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);

			.xztitle {
				position: fixed;
				left: 0%;
				top: 0;
				width: 100%;
				text-align: left;
				color: #333333;
				font-size: 32rpx;
				padding-left: 74rpx;
				font-weight: 700;

				text {
					font-size: 24rpx;
				}

				.iconfont {
					position: absolute;
					top: 0;
					left: 30rpx;
					font-size: 44rpx;
				}
			}
		}

		.headtop {
			width: 100%;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);
			height: 300rpx;
			box-sizing: border-box;
		}
		
		.beijing{
			width: 90%;
			margin: 0 auto;
			margin-top: -280rpx;
			position: relative;
			
			image{
				width: 100%;
				height: 360rpx;
			}
			
			.topcontent{
				position: absolute;
				width: 100%;
				top: 0;
				left: 0;
				padding: 30rpx;
				box-sizing: border-box;
				
				.top{
					
					.title{
						font-size: 24rpx;
						color: #FFFFFF;
						height: 44rpx;
						line-height: 44rpx;
					}
					
					.tixian{
						font-size: 24rpx;
						color: #FFFFFF;
						padding: 6rpx 20rpx;
						background: rgba(255, 255, 255,0.2);
						border-radius: 24rpx;
						margin-left: 220rpx;
					}
					
					.jilu{
						font-size: 24rpx;
						color: #FFFFFF;
						padding: 6rpx 20rpx;
						background: rgba(255, 255, 255,0.2);
						border-radius: 24rpx;
						margin-left: 20rpx;
					}
				}
				
				.money{
					margin-top: 10rpx;
					font-size: 72rpx;
					color: #FFFFFF;
					font-weight: 700;
				}
				
				.bottom{
					width: 84%;
					margin: 0 auto;
					margin-top: 20rpx;
					background: #F6F8FF;
					border-radius: 20rpx;
					
					.kuai{
						width: 50%;
						text-align: center;
						
						.shuzi{
							margin-top: 20rpx;
							font-size: 48rpx;
							color: #000000;
						}
						
						.desc{
							font-size: 24rpx;
							color: #333333;
							margin-bottom: 20rpx;
						}
					}
				}
			}
		}
		
		.mendian{
			width: 90%;
			margin: 0 auto;
			z-index: 70;
			margin-top: 20rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			box-shadow: rgba(223, 230, 255, 0.67) 0px 0px 6px;
			
			.heads{
				width: 100%;
				padding: 24rpx 20rpx 0;
				box-sizing: border-box;
				
				.left{
					
					image{
						float: left;
						width: 40rpx;
						height: 40rpx;
						margin-top: -6rpx;
					}
					
					.name{
						font-size: 28rpx;
						color: #333333;
						float: left;
					}
				}
				
				.right{
					
					image{
						float: left;
						width: 40rpx;
						height: 40rpx;
						margin-top: -6rpx;
					}
					
					.name{
						font-size: 28rpx;
						color: #333333;
						float: left;
					}
				}
			}
			
			.date{
				width: 100%;
				padding: 20rpx 20rpx 0;
				text-align: right;
				font-size: 24rpx;
				color: #5C88FA;
			}
			
			.shuju{
				width: 100%;
				padding: 30rpx 26rpx;
				box-sizing: border-box;
				
				.li{
					width: 100%;
					position: relative;
					padding: 30rpx;
					margin-bottom: 20rpx;
					background: #F6F8FF;
					border-radius: 16rpx;
					
					image{
						width: 44rpx;
						height: 44rpx;
						position: absolute;
						top: 46rpx;
						right: 30rpx;
					}
					
					.cen{
						width: 430rpx;
						
						text{
							display: block;
						}
						
						.title{
							font-size: 28rpx;
							color: #333333;
							margin-bottom: 6rpx;
						}
						
						.time{
							font-size: 24rpx;
							color: #999999;
						}
					}
					
					.name{
						height: 76rpx;
						line-height: 76rpx;
						font-size: 36rpx;
						color: #333333;
						font-weight: 700;
					}
				}
			}
		}
		
		.empty{
			width: 100%;
			margin: 20rpx auto 0;
			text-align: center;
			padding-bottom: 20rpx;
			
			image{
				width: 390rpx;
				height: 340rpx;
			}
			
			text{
				display: block;
				margin-top: 30rpx;
				font-size: 24rpx;
				color: #666666;
			}
		}
		
		.footer {
			width: 100%;
			margin: 0 auto 40rpx;
			margin-top: 100rpx;
			height: 72rpx;
			text-align: center;
		
			.submit {
				width: 240rpx;
				height: 72rpx;
				line-height: 72rpx;
				background: linear-gradient(90deg, #65bdfc, #398ef8);
				margin-left: 248rpx;
				border-radius: 36rpx;
				border-top-left-radius: 0;
				border-bottom-left-radius: 42rpx;
				color: #FFFFFF;
				font-size: 32rpx;
			}
		}
	}
</style>